import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'native_view_platform_interface.dart';

/// An implementation of [NativeViewPlatform] that uses method channels.
class MethodChannelNativeView extends NativeViewPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('native_view');

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  // 方法1: 使用 createSurfaceTexture (标准方式)
   @override
  Future<int> createSurfaceTextureDemo() async {
    try {
      final textureId = await methodChannel.invokeMethod('createSurfaceTextureDemo');
      return textureId;
    } on PlatformException catch (e) {
      print("Failed to create surface texture: ${e.message}");
      return -1;
    }
  }

  // 方法2: 使用 createSurfaceProducer (底层方式)
   @override
  Future<int> createSurfaceProducerDemo() async {
    try {
      final textureId = await methodChannel.invokeMethod('createSurfaceProducerDemo');
      return textureId;
    } on PlatformException catch (e) {
      if (kDebugMode) {
        print("Failed to create surface producer: ${e.message}");
      }
      return -1;
    }
  }

  // 方法3: 使用手动 SurfaceTexture (自定义方式)
   @override
  Future<int> createManualSurfaceTextureDemo() async {
    try {
      final textureId = await methodChannel.invokeMethod('createManualSurfaceTextureDemo');
      return textureId;
    } on PlatformException catch (e) {
      print("Failed to create manual surface texture: ${e.message}");
      return -1;
    }
  }

  // 释放纹理资源
   @override
  Future<void> disposeTexture(int textureId) async {
    try {
      await methodChannel.invokeMethod('disposeTexture', {'textureId': textureId});
    } on PlatformException catch (e) {
      print("Failed to dispose texture: ${e.message}");
    }
  }

}
